
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>jQuery.dataTables 自定义排序 | Jark&#39;s Blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="WuChong">
    
    <meta name="description" itemprop="description" content="最近在项目中用到了 jQuery.dataTables，这是一个很强大的 jQuery 表格插件，自带了 string，date，numeric 的排序，但当遇到比较特殊的排序需求时，就得另寻出路了。本文根据官方文档简述了如何用 dataTables 根据具体的列来定制排序方法。">
    
    
    <meta name="description" content="最近在项目中用到了 jQuery.dataTables，这是一个很强大的 jQuery 表格插件，自带了 string，date，numeric 的排序，但当遇到比较特殊的排序需求时，就得另寻出路了。本文根据官方文档简述了如何用 dataTables 根据具体的列来定制排序方法。">
<meta property="og:type" content="article">
<meta property="og:title" content="jQuery.dataTables 自定义排序">
<meta property="og:url" content="http://wuchong.me/blog/2014/02/25/jquery-datatable-sort-plugin/">
<meta property="og:site_name" content="Jark's Blog">
<meta property="og:description" content="最近在项目中用到了 jQuery.dataTables，这是一个很强大的 jQuery 表格插件，自带了 string，date，numeric 的排序，但当遇到比较特殊的排序需求时，就得另寻出路了。本文根据官方文档简述了如何用 dataTables 根据具体的列来定制排序方法。">
<meta property="og:image" content="/img/20140225-1.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="jQuery.dataTables 自定义排序">
<meta name="twitter:description" content="最近在项目中用到了 jQuery.dataTables，这是一个很强大的 jQuery 表格插件，自带了 string，date，numeric 的排序，但当遇到比较特殊的排序需求时，就得另寻出路了。本文根据官方文档简述了如何用 dataTables 根据具体的列来定制排序方法。">
<meta name="twitter:creator" content="@jarkwu">
<link rel="publisher" href="111190881341800841449">

    
    <link rel="alternative" href="/atom.xml" title="Jark&#39;s Blog" type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/jacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css" type="text/css">
</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.png" alt="Jark&#39;s Blog" title="Jark&#39;s Blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Jark&#39;s Blog">Jark&#39;s Blog</a></h1>
				<h2 class="blog-motto">当你的才华还撑不起你的野心时，你就应该静下心来学习。</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">首页</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/about">关于</a></li>
					
					<li>
 					
						<form class="search" action="http://zhannei.baidu.com/cse/search" target="_blank">
							<label>Search</label>
						<input name="s" type="hidden" value= 783281470518440642 ><input type="text" name="q" size="30" placeholder="搜索"><br>
						</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/blog/2014/02/25/jquery-datatable-sort-plugin/" title="jQuery.dataTables 自定义排序" itemprop="url">jQuery.dataTables 自定义排序</a>
  </h1>
  <p class="article-author">By
       
		<a href="https://plus.google.com/111190881341800841449?rel=author" title="WuChong" target="_blank" itemprop="author">WuChong</a>
		
  <p class="article-time">
    <time datetime="2014-02-25T14:33:30.000Z" itemprop="datePublished"> 发表于 2014-02-25</time>
    
  </p>
</header>
	<div class="article-content">
		
		<p>最近在项目中用到了 jQuery.dataTables， 这是一个很强大的 jQuery 插件，调用方便，支持回调对数据进行排序、查询、分页等操作，并且 bootstrap 框架也有对其封装，省了我们界面设计的活。dataTables 自带了string，date，numeric 的排序，但当遇到比较特殊的排序需求时，就得另寻出路了。</p>
<p>这几天正好碰到了这么个需求，一个统计表格中的有几列的单元格是百分数，而且还带有 html 的标签。需要针对这几列的百分数排序。</p>
<p>单元格中的 html 代码是这样的。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre>1
2
</pre></td><td class="code"><pre><span class="tag">&lt;<span class="title">span</span> <span class="attribute">class</span>=<span class="value">"label label-important"</span>&gt;</span>
<span class="tag">&lt;<span class="title">i</span> <span class="attribute">class</span>=<span class="value">"icon-caret-up"</span>&gt;</span><span class="tag">&lt;/<span class="title">i</span>&gt;</span>&amp;nbsp;&amp;nbsp;100.0%<span class="tag">&lt;/<span class="title">span</span>&gt;</span>
</pre></td></tr></table></figure>

<p>表格大概是长这样的。</p>
<p><img src="/img/20140225-1.png" alt=""></p>
<p>而 dataTables 的自带排序会将这一列视为 string 排序。 显然是不满足我们需求的。一开始以为要大动干戈，后来看了API文档后发现，dataTables 的第三方扩展支持还是很灵活的。官方文档中提供了两种方法： (1) Type based column sorting ; (2) Custom data source sorting<br>。我使用的是第一种方法。主要思路就是将单元格内容转成可排序的 float 类型。<br><a id="more"></a></p>
<ol>
<li><p>首先创建一个文件叫<code>dataTables.sort.plungin.js</code>，加入以下代码。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre>jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    <span class="string">"html-percent-pre"</span>: <span class="function"><span class="keyword">function</span> <span class="params">(a)</span> {</span>
        <span class="keyword">var</span> x = <span class="built_in">String</span>(a).replace(<span class="regexp">/&lt;[\s\S]*?&gt;/g</span>, <span class="string">""</span>);    <span class="comment">//去除html标记</span>
        x = x.replace(<span class="regexp">/&amp;nbsp;/ig</span>, <span class="string">""</span>);                   <span class="comment">//去除空格</span>
        x = x.replace(<span class="regexp">/%/</span>, <span class="string">""</span>);                          <span class="comment">//去除百分号</span>
        <span class="keyword">return</span> <span class="built_in">parseFloat</span>(x);
    },

    <span class="string">"html-percent-asc"</span>: <span class="function"><span class="keyword">function</span> <span class="params">(a, b)</span> {</span>                <span class="comment">//正序排序引用方法</span>
        <span class="keyword">return</span> ((a &lt; b) ? -<span class="number">1</span> : ((a &gt; b) ? <span class="number">1</span> : <span class="number">0</span>));
    },

    <span class="string">"html-percent-desc"</span>: <span class="function"><span class="keyword">function</span> <span class="params">(a, b)</span> {</span>                <span class="comment">//倒序排序引用方法</span>
        <span class="keyword">return</span> ((a &lt; b) ? <span class="number">1</span> : ((a &gt; b) ? -<span class="number">1</span> : <span class="number">0</span>));
    }
});
</pre></td></tr></table></figure>
</li>
<li><p>在前台页面中加入以下的 js 引用。 </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre><span class="tag">&lt;<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"text/javascript"</span> <span class="attribute">src</span>=<span class="value">"jquery.dataTables.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span>
<span class="tag">&lt;<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"text/javascript"</span> <span class="attribute">src</span>=<span class="value">"dataTables.numericComma.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span> 
<span class="tag">&lt;<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"text/javascript"</span>&gt;</span><span class="javascript">
            <span class="keyword">var</span> oTable1 = $(<span class="string">'#table_report'</span>).dataTable({
                <span class="string">"aoColumnDefs"</span>: [
                    { <span class="string">"sType"</span>: <span class="string">"html-percent"</span>, <span class="string">"aTargets"</span>: [<span class="number">8</span>] },    <span class="comment">//指定列号使用自定义排序</span>
                ],
                <span class="string">"bLengthChange"</span>: <span class="literal">true</span>, <span class="comment">//开关，是否显示每页大小的下拉框</span>
                <span class="string">"aLengthMenu"</span>: [[<span class="number">5</span>, <span class="number">10</span>, <span class="number">25</span>, -<span class="number">1</span>], [<span class="number">5</span>, <span class="number">10</span>, <span class="number">25</span>, <span class="string">"所有"</span>]],
                <span class="string">'iDisplayLength'</span>: <span class="number">25</span>, <span class="comment">//每页显示10条记录</span>
                <span class="string">'bFilter'</span>: <span class="literal">true</span>,  <span class="comment">//是否使用内置的过滤功能</span>
                <span class="string">"bInfo"</span>: <span class="literal">true</span>, <span class="comment">//开关，是否显示表格的一些信息</span>
                <span class="string">"bPaginate"</span>: <span class="literal">true</span> <span class="comment">//开关，是否显示分页器</span>
            });
        });
</span><span class="tag">&lt;/<span class="title">script</span>&gt;</span>
</pre></td></tr></table></figure>
</li>
<li><p>完成。</p>
</li>
</ol>
<h2 id="参考资料">参考资料</h2>
<ul>
<li><a href="https://www.datatables.net/development/sorting" target="_blank" rel="external">DataTables关于排序的文档</a></li>
<li><a href="http://datatables.net/plug-ins/sorting#how_to_data_source" target="_blank" rel="external">另一种自定义方法</a></li>
</ul>
<p>-EOF-</p>
  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/编程语言/">编程语言</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/tags/jQuery/">jQuery</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://wuchong.me/blog/2014/02/25/jquery-datatable-sort-plugin/" data-title="jQuery.dataTables 自定义排序 | Jark&#39;s Blog" data-tsina="2176287895" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/blog/2014/03/14/use-latex-in-blog/" title="在博客上写LaTex数学公式">
  <strong>上一篇：</strong><br/>
  <span>
  在博客上写LaTex数学公式</span>
</a>
</div>


<div class="next">
<a href="/blog/2014/02/09/algorithm-sort-summary/"  title="经典排序算法总结与实现">
 <strong>下一篇：</strong><br/> 
 <span>经典排序算法总结与实现
</span>
</a>
</div>

</nav>

	
<section id="comments" class="comment">
	<div class="ds-thread" data-thread-key="blog/2014/02/25/jquery-datatable-sort-plugin/" data-title="jQuery.dataTables 自定义排序" data-url="http://wuchong.me/blog/2014/02/25/jquery-datatable-sort-plugin/"></div>
</section>


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  
<div class="categorieslist">
	<p class="asidetitle">分类</p>
		<ul>
		
			<li><a href="/categories/Hexo/" title="Hexo">Hexo<sup>7</sup></a></li>
		
			<li><a href="/categories/LinuxUnix/" title="Linux/Unix">Linux/Unix<sup>6</sup></a></li>
		
			<li><a href="/categories/Python/" title="Python">Python<sup>3</sup></a></li>
		
			<li><a href="/categories/推荐系统/" title="推荐系统">推荐系统<sup>3</sup></a></li>
		
			<li><a href="/categories/杂项资源/" title="杂项资源">杂项资源<sup>2</sup></a></li>
		
			<li><a href="/categories/程序设计/" title="程序设计">程序设计<sup>5</sup></a></li>
		
			<li><a href="/categories/系统架构/" title="系统架构">系统架构<sup>1</sup></a></li>
		
			<li><a href="/categories/编程语言/" title="编程语言">编程语言<sup>2</sup></a></li>
		
			<li><a href="/categories/职场生涯/" title="职场生涯">职场生涯<sup>2</sup></a></li>
		
			<li><a href="/categories/随笔生活/" title="随笔生活">随笔生活<sup>5</sup></a></li>
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">标签</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Hexo/" title="Hexo">Hexo<sup>8</sup></a></li>
		
			<li><a href="/tags/面试/" title="面试">面试<sup>5</sup></a></li>
		
			<li><a href="/tags/Linux基础/" title="Linux基础">Linux基础<sup>5</sup></a></li>
		
			<li><a href="/tags/生活/" title="生活">生活<sup>4</sup></a></li>
		
			<li><a href="/tags/博客/" title="博客">博客<sup>4</sup></a></li>
		
			<li><a href="/tags/Python/" title="Python">Python<sup>4</sup></a></li>
		
			<li><a href="/tags/阿里推荐大赛/" title="阿里推荐大赛">阿里推荐大赛<sup>4</sup></a></li>
		
			<li><a href="/tags/算法/" title="算法">算法<sup>2</sup></a></li>
		
			<li><a href="/tags/爬虫/" title="爬虫">爬虫<sup>2</sup></a></li>
		
			<li><a href="/tags/ODPS/" title="ODPS">ODPS<sup>2</sup></a></li>
		
			<li><a href="/tags/教程/" title="教程">教程<sup>2</sup></a></li>
		
			<li><a href="/tags/Jacman/" title="Jacman">Jacman<sup>2</sup></a></li>
		
			<li><a href="/tags/Java/" title="Java">Java<sup>2</sup></a></li>
		
			<li><a href="/tags/文件系统/" title="文件系统">文件系统<sup>2</sup></a></li>
		
			<li><a href="/tags/启动流程/" title="启动流程">启动流程<sup>1</sup></a></li>
		
			<li><a href="/tags/权限/" title="权限">权限<sup>1</sup></a></li>
		
			<li><a href="/tags/正则表达式/" title="正则表达式">正则表达式<sup>1</sup></a></li>
		
			<li><a href="/tags/jQuery/" title="jQuery">jQuery<sup>1</sup></a></li>
		
			<li><a href="/tags/双检锁/" title="双检锁">双检锁<sup>1</sup></a></li>
		
			<li><a href="/tags/设计模式/" title="设计模式">设计模式<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">友情链接</p>
    <ul>
        
          <li>
            <a href="https://coderq.com" target="_blank" title="码农圈">码农圈</a>
          </li>
        
          <li>
            <a href="http://zipperary.com/" target="_blank" title="Zippera&#39;s Blog">Zippera&#39;s Blog</a>
          </li>
        
          <li>
            <a href="http://hijiangtao.github.io/" target="_blank" title="Data.Blog">Data.Blog</a>
          </li>
        
    </ul>
</div>

  <div class="weiboshow">
  <p class="asidetitle">新浪微博</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=2176287895&verifier=b3593ceb&dpc=1"></iframe>
</div>


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello,I&#39;m WuChong. For now I&#39;m a graduate student in Beijing. <br/>
			I&#39;ll share my learning experience with you at this blog.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		<a href="http://weibo.com/wuchong1014" target="_blank" class="icon-weibo" title="微博"></a>
		
		
		<a href="https://github.com/wuchong" target="_blank" class="icon-github" title="github"></a>
		
		
		
		<a href="https://twitter.com/jarkwu" target="_blank" class="icon-twitter" title="twitter"></a>
		
		
		
		
		
		
		<a href="https://plus.google.com/111190881341800841449?rel=author" target="_blank" class="icon-google_plus" title="Google+"></a>
		
		
	</div>
		<p class="copyright">Powered by <a href="http://zespia.tw/hexo/" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Pacman">Jacman</a> © 2014 
		
		<a href="http://wuchong.me/about" target="_blank" title="WuChong">WuChong</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.imagesloaded.min.js"></script>
<script src="/js/gallery.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#nothing"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>



<script type="text/javascript">
  var duoshuoQuery = {short_name:"wuchong"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
    || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 







<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->

<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46321946-2', 'wuchong.me');  
ga('send', 'pageview');
</script>





<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="返回顶部"><img src="/img/scrollup.png"/></a>
	</div>
	<script src="/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

  </body>
</html>
